package com.itheima.health.service.impl;


import com.itheima.health.dao.ReportDao;

import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhangmeng
 * @description
 * @date 2019/9/18
 **/
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;

    @Transactional
    @Override
    public Map<String, Object> selectMemberCount() {
        Calendar instance = Calendar.getInstance();
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> months = new ArrayList<>();
        ArrayList<Integer> memberCount = new ArrayList<>();
        int m = instance.get(Calendar.MONTH) + 2;
        int y = instance.get(Calendar.YEAR) - 1;
        for (int i = 0; i < 12; i++) {
            int month = m + i;
            int year = y;
            if (m + i > 12) {
                year = y + 1;
                month = m + i - 12;
            }
            String monthstr = year + "-" + month;
            Integer count = reportDao.selectMemberCount(monthstr);
            months.add(monthstr);
            memberCount.add(count);
        }
        map.put("months", months);
        map.put("memberCount", memberCount);
        System.out.println(months);
        System.out.println(memberCount);
        return map;
    }

    @Transactional
    @Override
    public Map<String, Object> selectSetmealCount() {
        ArrayList<String> nameList = new ArrayList<>();
        ArrayList<Map<String, Object>> countList = new ArrayList<>();
        HashMap<String, Object> resultMap = new HashMap<>();
        List<Setmeal> setmealList = reportDao.findSetmealAll();
        for (Setmeal setmeal : setmealList) {
            Integer id = setmeal.getId();
            Integer setmealCount = reportDao.findSetmealCount(id);
            if (setmealCount > 0) {
                nameList.add(setmeal.getName());
                HashMap<String, Object> countMap = new HashMap<>();
                countMap.put("name", setmeal.getName());
                countMap.put("value", setmealCount);
                countList.add(countMap);
            }
        }
        resultMap.put("setmealNames", nameList);
        resultMap.put("setmealCount", countList);
        return resultMap;
    }

    @Transactional
    @Override
    public Map<String, Object> selectBuinessReport() {
        HashMap<String, Object> map = new HashMap<>();

        Date date = new Date();
        String reportDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        map.put("reportDate",reportDate);

        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date week = cal.getTime();
        String weekstart = new SimpleDateFormat("yyyy-MM-dd").format(week);
        Calendar cal1 = Calendar.getInstance();
        cal1.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONDAY), cal1.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal1.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date months = cal1.getTime();
        String monthstart = new SimpleDateFormat("yyyy-MM-dd").format(months);

        Integer todayNewMember = reportDao.getTodayNewMember(reportDate);
        Integer thisMonthNewMember = reportDao.getThisMonthNewMember(monthstart);
        Integer thisWeekNewMember = reportDao.getThisWeekNewMember(weekstart);
        Integer totalMember = reportDao.getTotalMember();
        map.put("todayNewMember", todayNewMember);
        map.put("thisMonthNewMember", thisMonthNewMember);
        map.put("thisWeekNewMember", thisWeekNewMember);
        map.put("totalMember", totalMember);

        Integer todayVisitsNumber = reportDao.getTodayVisitsNumber(reportDate);
        Integer todayOrderNumber = reportDao.getTodayOrderNumber(reportDate);
        Integer thisMonthOrderNumber = reportDao.getThisMonthOrderNumber(monthstart);
        Integer thisMonthVisitsNumber = reportDao.getThisMonthVisitsNumber(monthstart);
        Integer thisWeekVisitsNumber = reportDao.getThisWeekVisitsNumber(weekstart);
        Integer thisWeekOrderNumber = reportDao.getThisWeekOrderNumber(weekstart);
        map.put("todayVisitsNumber", todayVisitsNumber);
        map.put("todayOrderNumber", todayOrderNumber);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);

        ArrayList<Object> hotSetmealList = new ArrayList<>();
        List<Map<Integer, Integer>> maxSetmealIdAndCount = reportDao.findMaxSetmealIdAndCount();
        for (Map<Integer, Integer> countMap : maxSetmealIdAndCount) {
            Integer setmeal_id = countMap.get("setmeal_id");
            Integer count = countMap.get("count");
            String setmealById = reportDao.findSetmealById(setmeal_id);
            HashMap<String, Object> hotSetmealMsg = new HashMap<>();
            hotSetmealMsg.put("name", setmealById);
            hotSetmealMsg.put("setmeal_count", count);
            Double setmealAllCount = reportDao.findSetmealAllCount();
            double v = count / setmealAllCount;
            BigDecimal b = new BigDecimal(v);
            double proportion = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
            hotSetmealMsg.put("proportion", proportion);
            hotSetmealList.add(hotSetmealMsg);
        }
        map.put("hotSetmeal", hotSetmealList);
        return map;
    }
}
